/**
 * @author 徐楠
 * @date 2021/11/18 10:56
 * @version 1.0
 */

package edu.nuist.javaProjct.xn;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

import static java.awt.image.ImageObserver.WIDTH;

public class MyTest {

    public final int processListMAXLength = 15;
    public PCBDao[] inputProcessList = new PCBDao[processListMAXLength];
    public PCBDao[] readyProcessList = new PCBDao[processListMAXLength];
    public PCBDao runProcess = new PCBDao();
    public PCBDao[] endProcessList = new PCBDao[processListMAXLength];
    public PCBDao[] blockingProcessList = new PCBDao[processListMAXLength];
    public PCBDao[] waitingRunningProcessList = new PCBDao[processListMAXLength];


    //进程状态
    public final String RUNNING = "RUNNING";
    public final String READY = "READY";
    public final String END = "END";
    public final String BLOCK = "BLOCK";


    //最大随着数容量
    int RandomMAXNumbers = 500;
    //定义一大小为4的物理块数组（4块物理块）
    PhysicalBlock[] physicalBlocks = new PhysicalBlock[4];
    //指令地址
    int[] ordersAddress = new int[RandomMAXNumbers];
    //程序计数器，用来记录当前指令的序号
    int pc;
    //缺页计数器，用来记录缺页的次数
    int n;
    //指令总长度
    int orderLength = 0;
    //页面最大容量
    int MaximumPageCapacity = 10;
    //内存块中系统占用到的位置
    int systemOccupancy = 0;


    //存地址值的数组开始索引
    int ordersAddressFromIndex = 0;
    //存地址值的数组结束索引
    int ordersAddressTOIndex = 0;

    //数组长度
    int processAddressLength0;
    int processAddressLength1;
    int processAddressLength2;
    int processAddressLength3;
    int processAddressLength4;

    //各进程名
    String processName0;
    String processName1;
    String processName2;
    String processName3;
    String processName4;


    //面板
    private JPanel jaaa;
    private JPanel jpanel1;
    private JPanel jpanel2;
    private JPanel jpanel3;
    private JLabel JLablel1;
    //输入框
    private JTextField InputBox11;
    private JTextField InputBox12;
    private JTextField InputBox13;
    private JTextField InputBox14;
    private JTextField InputBox15;

    private JTextField InputBox21;
    private JTextField InputBox22;
    private JTextField InputBox23;
    private JTextField InputBox24;
    private JTextField InputBox25;

    private JTextField InputBox31;
    private JTextField InputBox32;
    private JTextField InputBox33;
    private JTextField InputBox34;
    private JTextField InputBox35;

    private JTextField InputBox41;
    private JTextField InputBox42;
    private JTextField InputBox43;
    private JTextField InputBox44;
    private JTextField InputBox45;

    private JTextField InputBox51;
    private JTextField InputBox52;
    private JTextField InputBox53;
    private JTextField InputBox54;
    private JTextField InputBox55;

    private JTextField InputBox61;
    private JTextField InputBox62;
    private JTextField InputBox63;
    private JTextField InputBox64;
    private JTextField InputBox65;


    //运行进程框
    private JTextField runningProcessBox1;
    private JTextField runningProcessBox2;
    private JTextField runningProcessBox3;
    private JTextField runningProcessBox4;
    private JTextField runningProcessBox5;
    private JTextField runningProcessBox6;

    //物理块
    private JTextField physicalBlock1;
    private JTextField physicalBlock2;
    private JTextField physicalBlock3;
    private JTextField physicalBlock4;
    //指令显示
    private JTextField orderFrame;

    //运行按钮
    private JButton runButton;
    //输入按钮
    private JButton inPutButton;
    //退出按钮
    private JButton quitButton;
    //页表
    private JPanel jshowPageTablePanel;
    private JList list1;
    private JList list2;
    private JList listMemory1;
    private JList listMemory2;

    private JPanel jMissingPagesPanel;
    private JTextField JMissingPagesNumberTestField;
    private JTextField JPageMissingRateTestField;
    private JPanel JEndProcessPanel;
    private JList listEndProcess1;
    private JList listEndProcess2;
    private JList listEndProcess3;
    private JList listEndProcess4;
    private JList listEndProcess5;
    private JList listEndProcess6;
    private JPanel JIncreaseProcessPanel;
    private JTextField textField1;
    private JTextField textField2;
    private JTextField textField3;
    private JTextField textField4;
    private JTextField textField5;
    private JButton increaseProcessButton;
    private JTextField textField6;
    private JList listBlockingProcess1;
    private JList listBlockingProcess2;
    private JList listBlockingProcess3;
    private JList listBlockingProcess6;
    private JList listBlockingProcess4;
    private JList listBlockingProcess5;
    private JList listWaitingProcess1;
    private JList listWaitingProcess2;
    private JList listWaitingProcess3;
    private JList listWaitingProcess4;
    private JList listWaitingProcess5;
    private JList listWaitingProcess6;
    private JTextField t1;
    private JTextField t2;
    private JTextField t3;
    private JTextField t4;

    public MyTest() {
        InputBox11.setText("tyy");
        InputBox12.setText("xxw");
        InputBox13.setText("cctv");
        InputBox14.setText("vvq");
        InputBox15.setText("baby");

        InputBox21.setText("9");
        InputBox22.setText("38");
        InputBox23.setText("30");
        InputBox24.setText("29");
        InputBox25.setText("0");

        InputBox31.setText("13");
        InputBox32.setText("8");
        InputBox33.setText("14");
        InputBox34.setText("11");
        InputBox35.setText("17");

        InputBox41.setText(READY);
        InputBox42.setText(READY);
        InputBox43.setText(READY);
        InputBox44.setText(READY);
        InputBox45.setText(READY);

        InputBox51.setText("0");
        InputBox52.setText("0");
        InputBox53.setText("0");
        InputBox54.setText("0");
        InputBox55.setText("0");

        InputBox61.setText("13");
        InputBox62.setText("8");
        InputBox63.setText("14");
        InputBox64.setText("11");
        InputBox65.setText("17");


        textField1.setText("mmm");
        textField2.setText("40");
        textField3.setText("5");
        textField4.setText(READY);
        textField5.setText("0");
        textField6.setText("5");

        //退出按钮绑定监听
        quitButton.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent evt) {
                System.exit(WIDTH);
            }
        });

        inPutButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                processName0 = InputBox11.getText();
                int priority1 = Integer.parseInt(InputBox21.getText());
                int CPURunningTime1 = Integer.parseInt(InputBox31.getText());
                String state1 = InputBox41.getText();
                int blockingTime1 = Integer.parseInt(InputBox51.getText());
                int orders1 = Integer.parseInt(InputBox61.getText());

                processName1 = InputBox12.getText();
                int priority2 = Integer.parseInt(InputBox22.getText());
                int CPURunningTime2 = Integer.parseInt(InputBox32.getText());
                String state2 = InputBox42.getText();
                int blockingTime2 = Integer.parseInt(InputBox52.getText());
                int orders2 = Integer.parseInt(InputBox62.getText());

                processName2 = InputBox13.getText();
                int priority3 = Integer.parseInt(InputBox23.getText());
                int CPURunningTime3 = Integer.parseInt(InputBox33.getText());
                String state3 = InputBox43.getText();
                int blockingTime3 = Integer.parseInt(InputBox53.getText());
                int orders3 = Integer.parseInt(InputBox63.getText());

                processName3 = InputBox14.getText();
                int priority4 = Integer.parseInt(InputBox24.getText());
                int CPURunningTime4 = Integer.parseInt(InputBox34.getText());
                String state4 = InputBox44.getText();
                int blockingTime4 = Integer.parseInt(InputBox54.getText());
                int orders4 = Integer.parseInt(InputBox64.getText());

                processName4 = InputBox15.getText();
                int priority5 = Integer.parseInt(InputBox25.getText());
                int CPURunningTime5 = Integer.parseInt(InputBox35.getText());
                String state5 = InputBox45.getText();
                int blockingTime5 = Integer.parseInt(InputBox55.getText());
                int orders5 = Integer.parseInt(InputBox65.getText());

                //初始化数组
                initialization();

                //获取面板数据
                inputProcessList[0].processName = processName0;
                inputProcessList[0].priority = priority1;
                inputProcessList[0].CPURunningTime = CPURunningTime1;
                inputProcessList[0].state = state1;
                inputProcessList[0].blockingTime = blockingTime1;
                inputProcessList[0].orders = orders1;
                inputProcessList[0].ordersLOCK = orders1;

                inputProcessList[1].processName = processName1;
                inputProcessList[1].priority = priority2;
                inputProcessList[1].CPURunningTime = CPURunningTime2;
                inputProcessList[1].state = state2;
                inputProcessList[1].blockingTime = blockingTime2;
                inputProcessList[1].orders = orders2;
                inputProcessList[1].ordersLOCK = orders2;

                inputProcessList[2].processName = processName2;
                inputProcessList[2].priority = priority3;
                inputProcessList[2].CPURunningTime = CPURunningTime3;
                inputProcessList[2].state = state3;
                inputProcessList[2].blockingTime = blockingTime3;
                inputProcessList[2].orders = orders3;
                inputProcessList[2].ordersLOCK = orders3;

                inputProcessList[3].processName = processName3;
                inputProcessList[3].priority = priority4;
                inputProcessList[3].CPURunningTime = CPURunningTime4;
                inputProcessList[3].state = state4;
                inputProcessList[3].blockingTime = blockingTime4;
                inputProcessList[3].orders = orders4;
                inputProcessList[3].ordersLOCK = orders4;

                inputProcessList[4].processName = processName4;
                inputProcessList[4].priority = priority5;
                inputProcessList[4].CPURunningTime = CPURunningTime5;
                inputProcessList[4].state = state5;
                inputProcessList[4].blockingTime = blockingTime5;
                inputProcessList[4].orders = orders5;
                inputProcessList[4].ordersLOCK = orders5;

                //长度赋值
                for (int i = 0; i < 5; i++) {
                    orderLength = orderLength + inputProcessList[i].orders;
                }
                //生成随机地址数组
                createRandomAddress();

                //每个进程地址数组赋值
                for (int i = 0; i < inputProcessList.length; i++) {
                    ordersAddressTOIndex = ordersAddressTOIndex + inputProcessList[i].orders;
                    inputProcessList[i].orderSet = Arrays.copyOfRange(ordersAddress, ordersAddressFromIndex, ordersAddressTOIndex);
                    ordersAddressFromIndex = ordersAddressTOIndex;
                }

                //给数组从大到小排序
                readyProcessListSort(inputProcessList);

                //指令数组排大小
                for (int i = 0; i < inputProcessList.length; i++) {
                    bubbleSorting(inputProcessList[i].orderSet);
                }

                System.out.println(inputProcessList[0]);
                System.out.println(inputProcessList[1]);
                System.out.println(inputProcessList[2]);


                //把inputProcessList赋值给readyProcessList
                for (int i = 0; i < processListMAXLength; i++) {
                    if (i < inputProcessList.length) {
                        readyProcessList[i] = inputProcessList[i];
                    } else {
                        readyProcessList[i] = new PCBDao();
                    }
                }

                //把readyProcessList中优先级最高且状态为ready的赋值给runProcess
                for (int i = 0; i < readyProcessList.length; i++) {
                    if (readyProcessList[i].state.equals(READY)) {
                        runProcess = readyProcessList[i];
                        break;
                    }
                }


                //将整在运行的程序的页表显示出来
                String[] runningProcessAddressArrayString = new String[runProcess.ordersLOCK];
                intArrayTOStringArray(runProcess.orderSet, runningProcessAddressArrayString);
                list1.setListData(runningProcessAddressArrayString);

                String[] runningProcessPageHome = new String[runProcess.ordersLOCK];
                InitializeProcessPageHome(runningProcessPageHome);

                //把数据展示到等待进程面板和运行进程面板
                int runProcessIndex = -1;
                for (int i = 0; i < readyProcessList.length; i++) {
                    if (readyProcessList[i] == runProcess && readyProcessList[i] != null) {
                        runProcessIndex = i;
                        break;
                    }
                }
                waitingRunningProcessList = removeArrayEmptyTextBackNewArray(readyProcessList, READY, runProcessIndex);
                //将waitingRunningProcessList显示在等待进程面板上
                showWaitingProcessBox();

                //将运行中阻塞的程序存入blockingProcessList
                blockingProcessList = removeArrayEmptyTextBackNewArray(readyProcessList, BLOCK, -1);
                //将阻塞的进程显示在面板
                showBlockingProcessBox();

                //显示运行框数据
                runningProcessBox1.setText(runProcess.processName);
                runningProcessBox2.setText(String.valueOf(runProcess.priority));
                runningProcessBox3.setText(String.valueOf(runProcess.CPURunningTime));
                runningProcessBox4.setText(RUNNING);
                runningProcessBox5.setText(String.valueOf(runProcess.blockingTime));
                runningProcessBox6.setText(String.valueOf(runProcess.orders));


                //内存选取物理块情况
                selectPhysicalBlockFromMemory();

                systemOccupancy++;

                for (int i = 0; i < 4; i++) {
                    inputProcessList[0].physicalBlocksIdentifier[i] = systemOccupancy++;
                    inputProcessList[1].physicalBlocksIdentifier[i] = systemOccupancy++;
                    inputProcessList[2].physicalBlocksIdentifier[i] = systemOccupancy++;
                    inputProcessList[3].physicalBlocksIdentifier[i] = systemOccupancy++;
                    inputProcessList[4].physicalBlocksIdentifier[i] = systemOccupancy++;
                }

            }
        });

        runButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                //运行进程
                if (runProcess != null && runProcess.CPURunningTime > 0 && runProcess.state != null && runProcess.state.equals(READY)) {
                    runProcess.priority = runProcess.priority - 3;
                    runProcess.CPURunningTime = runProcess.CPURunningTime - 1;
                    runProcess.orders = runProcess.orders - 1;

                    for (int i = 0; i < readyProcessList.length; i++) {
                        if (runProcess.processName.equals(readyProcessList[i].processName)) {
                            LRU(runProcess.orderSet[runProcess.processAddressArrayIndex++]);
                        }
                    }

                    //显示物理块到面板
                    showPhysicalBlock();

                    //输入与输出变成BLOCK，阻塞时间为2个时间片
                    //runProcess运行后菜显示指令执行的内容，而runProcess显示的已经变成下一个准备执行的东西
                    String orderContent = ordersExecution();
                    if (orderContent.equals("INPUT") || orderContent.equals("OUTPUT")) {
                        runProcess.state = BLOCK;
                        runProcess.blockingTime = 2;
                    }

                    //设置页表中页号对应的物理块号
                    if (runProcess != null && !runProcess.processName.equals("NONE")) {
                        String[] runningProcessAddressArrayString = new String[runProcess.ordersLOCK];
                        System.out.println(runProcess.ordersLOCK);
                        intArrayTOStringArray(runProcess.orderSet, runningProcessAddressArrayString);
                        list1.setListData(runningProcessAddressArrayString);
                        String[] runningProcessPageHome = new String[runProcess.ordersLOCK];
                        changeProcessPageHome(runProcess.orderSet, runningProcessPageHome);
                        list2.setListData(runningProcessPageHome);

                    }
                }

                //等待进程优先级+1，如果是BLOCK进程：blocking时间 - 1
                for (int i = 0; i < readyProcessList.length; i++) {

                    if (runProcess == readyProcessList[i]) {
                        continue;
                    }
                    if (readyProcessList[i] == null) {
                        continue;
                    }
                    if (readyProcessList[i].state == null) {
                        continue;
                    }
                    if (readyProcessList[i].state.equals(READY)) {
                        readyProcessList[i].priority = readyProcessList[i].priority + 1;
                    } else if (readyProcessList[i].state.equals(BLOCK)) {
                        readyProcessList[i].priority = readyProcessList[i].priority + 1;
                        readyProcessList[i].blockingTime = readyProcessList[i].blockingTime - 1;
                    } else if (readyProcessList[i].state.equals(END)) {
                        continue;
                    }
                }

                //处理进程blocking时间、CPU运行时间、指令
                for (int i = 0; i < readyProcessList.length; i++) {

                    if (readyProcessList[i].blockingTime <= 0) {
                        readyProcessList[i].blockingTime = 0;
                    }
                    if (readyProcessList[i].CPURunningTime <= 0) {
                        readyProcessList[i].CPURunningTime = 0;
                    }
                    if (readyProcessList[i].orders <= 0) {
                        readyProcessList[i].orders = 0;
                    }
                }

                //处理进程状态
                for (int i = 0; i < readyProcessList.length; i++) {

                    if (readyProcessList[i].blockingTime == 0) {
                        readyProcessList[i].state = READY;
                    }
                    if (readyProcessList[i].CPURunningTime == 0) {
                        readyProcessList[i].state = END;
                    }
                }



                //给数组从大到小排序
                readyProcessListSort(readyProcessList);

                runProcess = null;

                for (int i = 0; i < readyProcessList.length; i++) {
                    if (readyProcessList[i].state.equals(READY)) {
                        runProcess = readyProcessList[i];
                        break;
                    }
                }

                //找到runProcess在readyProcessList中的位置
                int runProcessIndex = -1;
                for (int i = 0; i < readyProcessList.length; i++) {
                    if (readyProcessList[i] == runProcess && readyProcessList[i] != null) {
                        runProcessIndex = i;
                        break;
                    }
                }

                //将等待运行的程序存入waitingRunningProcessList
                waitingRunningProcessList = removeArrayEmptyTextBackNewArray(readyProcessList, READY, runProcessIndex);

                //将运行结束后的程序存入endProcessList
                endProcessList = removeArrayEmptyTextBackNewArray(readyProcessList, END, -1);
                /*System.out.println("////////////////////////////////");
                for (PCBDao pcbDao : endProcessList) {
                    System.out.println(pcbDao);
                }
                System.out.println("////////////////////////////////");*/

                //将运行中阻塞的程序存入blockingProcessList
                blockingProcessList = removeArrayEmptyTextBackNewArray(readyProcessList, BLOCK, -1);


                //将等待的进程显示在面板
                showWaitingProcessBox();
                //将阻塞的进程显示在面板
                showBlockingProcessBox();
                //将运行结束的进程显示在面板
                showEndProcessBox();
                //显示运行的程序到面板
                if (runProcess == null) {
                    runningProcessBox1.setText("-");
                    runningProcessBox2.setText("-");
                    runningProcessBox3.setText("-");
                    runningProcessBox4.setText("-");
                    runningProcessBox5.setText("-");
                    runningProcessBox6.setText("-");
                } else {
                    runningProcessBox1.setText(runProcess.processName);
                    runningProcessBox2.setText(String.valueOf(runProcess.priority));
                    runningProcessBox3.setText(String.valueOf(runProcess.CPURunningTime));
                    runningProcessBox4.setText(RUNNING);
                    runningProcessBox5.setText(String.valueOf(runProcess.blockingTime));
                    runningProcessBox6.setText(String.valueOf(runProcess.orders));
                }


                //显示缺页情况
                JMissingPagesNumberTestField.setText(n + "");
                JPageMissingRateTestField.setText(((n * 0.1) / (orderLength * 0.1)) + "");

                //全部运行完后的处理
                if (runProcessIndex == -1) {

                    runProcess = new PCBDao();
                    runProcess.processName = "NONE";
                    runProcess.priority = -1;
                    runProcess.CPURunningTime = -1;
                    runProcess.state = "FINISH_ALL";
                    runProcess.blockingTime = -1;
                    runProcess.orders = -1;
                }

                if (runProcessIndex == -1 && blockingProcessList.length == 0) {

                    System.out.println("*************");
                    System.out.println("执行完了所有程序");
                    System.out.println("*************");
                }


            }
        });

        increaseProcessButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                int actuallyEffectiveProcessNumber = 0;
                for (int i = 0; i < readyProcessList.length; i++) {
                    if (readyProcessList[i].processName != null) {
                        actuallyEffectiveProcessNumber++;
                    }
                }
                readyProcessList[actuallyEffectiveProcessNumber].processName = textField1.getText();
                readyProcessList[actuallyEffectiveProcessNumber].priority = Integer.valueOf(textField2.getText());
                readyProcessList[actuallyEffectiveProcessNumber].CPURunningTime = Integer.valueOf(textField3.getText());
                readyProcessList[actuallyEffectiveProcessNumber].state = textField4.getText();
                readyProcessList[actuallyEffectiveProcessNumber].blockingTime = Integer.valueOf(textField5.getText());
                readyProcessList[actuallyEffectiveProcessNumber].orders = Integer.valueOf(textField6.getText());
                readyProcessList[actuallyEffectiveProcessNumber].processAddressArrayIndex = 0;
                readyProcessList[actuallyEffectiveProcessNumber].ordersLOCK = Integer.valueOf(textField6.getText());


                ordersAddressTOIndex = ordersAddressTOIndex + inputProcessList[actuallyEffectiveProcessNumber].orders;
                inputProcessList[actuallyEffectiveProcessNumber].orderSet = Arrays.copyOfRange(ordersAddress, ordersAddressFromIndex, ordersAddressTOIndex);
                ordersAddressFromIndex = ordersAddressTOIndex;

                bubbleSorting(readyProcessList[actuallyEffectiveProcessNumber].orderSet);

                for (int i = 0; i < 4; i++) {
                    inputProcessList[actuallyEffectiveProcessNumber].physicalBlocksIdentifier[i] = systemOccupancy++;
                }

                System.out.println("============readyProcessList[actuallyEffectiveProcessNumber]===========");
                System.out.println(readyProcessList[actuallyEffectiveProcessNumber]);
                System.out.println("============inputProcessList[actuallyEffectiveProcessNumber]===========");
                System.out.println(inputProcessList[actuallyEffectiveProcessNumber]);
                //给数组从大到小排序
                readyProcessListSort(readyProcessList);

                runProcess = null;

                for (int i = 0; i < readyProcessList.length; i++) {
                    if (readyProcessList[i].state.equals(READY)) {
                        runProcess = readyProcessList[i];
                        break;
                    }
                }
                //找到runProcess在readyProcessList中的位置
                int runProcessIndex = -1;
                for (int i = 0; i < readyProcessList.length; i++) {
                    if (readyProcessList[i] == runProcess && readyProcessList[i] != null) {
                        runProcessIndex = i;
                        break;
                    }
                }
                //将等待运行的程序存入waitingRunningProcessList
                waitingRunningProcessList = removeArrayEmptyTextBackNewArray(readyProcessList, READY, runProcessIndex);
                //将等待的进程显示在面板
                showWaitingProcessBox();
                //显示运行的程序到面板
                if (runProcess == null) {
                    runningProcessBox1.setText("-");
                    runningProcessBox2.setText("-");
                    runningProcessBox3.setText("-");
                    runningProcessBox4.setText("-");
                    runningProcessBox5.setText("-");
                    runningProcessBox6.setText("-");
                } else {
                    runningProcessBox1.setText(runProcess.processName);
                    runningProcessBox2.setText(String.valueOf(runProcess.priority));
                    runningProcessBox3.setText(String.valueOf(runProcess.CPURunningTime));
                    runningProcessBox4.setText(RUNNING);
                    runningProcessBox5.setText(String.valueOf(runProcess.blockingTime));
                    runningProcessBox6.setText(String.valueOf(runProcess.orders));
                }
            }
        });


    }

    //初始化
    public void initialization() {
        for (int i = 0; i < processListMAXLength; i++) {
            inputProcessList[i] = new PCBDao();

        }

        for (int i = 0; i < 4; i++) {
            physicalBlocks[i] = new PhysicalBlock();
            physicalBlocks[i].pageNum = -1;
            physicalBlocks[i].nextAccessInstruction = 0;
        }
        pc = 0;
        n = 0;
        orderLength = 0;

        processAddressLength0 = 0;
        processAddressLength1 = 0;
        processAddressLength2 = 0;
        processAddressLength3 = 0;
        processAddressLength4 = 0;
    }

    //整合数组，去除空的内容
    public PCBDao[] removeArrayEmptyTextBackNewArray(PCBDao[] strArray, String judgeState, int runningProcessIndex) {

        List<PCBDao> strList = Arrays.asList(strArray);
        List<PCBDao> strListNew = new ArrayList<>();

        for (int i = 0; i < strList.size(); i++) {
            if (strList.get(i).processName == null) {
                continue;
            }
            if (runningProcessIndex == i) {
                continue;
            }
            if (strList.get(i).state == null) {
                continue;
            }

            if (strList.get(i) != null && strList.get(i).state.equals(judgeState) && runningProcessIndex != i) {
                strListNew.add(strList.get(i));
            }

        }

        PCBDao[] strNewArray = strListNew.toArray(new PCBDao[strListNew.size()]);
        return strNewArray;

    }

    //从内存中选取物理块
    public void choosePhysicalBlocksByMemory() {
        Random random = new Random();
        systemOccupancy = (int) Math.round(Math.random() * (11 - 5) + 5);
    }

    //给数组从大到小排序
    public void readyProcessListSort(PCBDao[] result) {
        for (int i = 1; i < result.length; i++) {
            for (int j = i; j > 0; j--) {
                if (result[j - 1].priority < result[j].priority) {//大的放前面
                    PCBDao tmp = result[j - 1];
                    result[j - 1] = result[j];
                    result[j] = tmp;
                }
            }
        }
    }

    //将等待的进程显示在面板
    public void showWaitingProcessBox() {

        String[] waitingRunningProcessNameArray = new String[processListMAXLength];
        String[] waitingRunningProcessPriorityArray = new String[processListMAXLength];
        String[] waitingRunningProcessCPURunningTimeArray = new String[processListMAXLength];
        String[] waitingRunningProcessStateArray = new String[processListMAXLength];
        String[] waitingRunningProcessBlockingTimeArray = new String[processListMAXLength];
        String[] waitingRunningProcessOrdersArray = new String[processListMAXLength];

        for (int i = 0; i < waitingRunningProcessList.length; i++) {
            waitingRunningProcessNameArray[i] = waitingRunningProcessList[i].processName;
            waitingRunningProcessPriorityArray[i] = String.valueOf(waitingRunningProcessList[i].priority);
            waitingRunningProcessCPURunningTimeArray[i] = String.valueOf(waitingRunningProcessList[i].CPURunningTime);
            waitingRunningProcessStateArray[i] = waitingRunningProcessList[i].state;
            waitingRunningProcessBlockingTimeArray[i] = String.valueOf(waitingRunningProcessList[i].blockingTime);
            waitingRunningProcessOrdersArray[i] = String.valueOf(waitingRunningProcessList[i].orders);
        }

        listWaitingProcess1.setListData(waitingRunningProcessNameArray);
        listWaitingProcess2.setListData(waitingRunningProcessPriorityArray);
        listWaitingProcess3.setListData(waitingRunningProcessCPURunningTimeArray);
        listWaitingProcess4.setListData(waitingRunningProcessStateArray);
        listWaitingProcess5.setListData(waitingRunningProcessBlockingTimeArray);
        listWaitingProcess6.setListData(waitingRunningProcessOrdersArray);


    }

    //将阻塞结束的进程显示在面板
    public void showBlockingProcessBox() {

        String[] blockingProcessNameArray = new String[processListMAXLength];
        String[] blockingProcessPriorityArray = new String[processListMAXLength];
        String[] blockingProcessCPURunningTimeArray = new String[processListMAXLength];
        String[] blockingProcessStateArray = new String[processListMAXLength];
        String[] blockingProcessBlockingTimeArray = new String[processListMAXLength];
        String[] blockingProcessOrdersArray = new String[processListMAXLength];

        for (int i = 0; i < blockingProcessList.length; i++) {
            blockingProcessNameArray[i] = blockingProcessList[i].processName;
            blockingProcessPriorityArray[i] = String.valueOf(blockingProcessList[i].priority);
            blockingProcessCPURunningTimeArray[i] = String.valueOf(blockingProcessList[i].CPURunningTime);
            blockingProcessStateArray[i] = blockingProcessList[i].state;
            blockingProcessBlockingTimeArray[i] = String.valueOf(blockingProcessList[i].blockingTime);
            blockingProcessOrdersArray[i] = String.valueOf(blockingProcessList[i].orders);
        }

        listBlockingProcess1.setListData(blockingProcessNameArray);
        listBlockingProcess2.setListData(blockingProcessPriorityArray);
        listBlockingProcess3.setListData(blockingProcessCPURunningTimeArray);
        listBlockingProcess4.setListData(blockingProcessStateArray);
        listBlockingProcess5.setListData(blockingProcessBlockingTimeArray);
        listBlockingProcess6.setListData(blockingProcessOrdersArray);

    }

    //将运行结束的进程显示在面板
    public void showEndProcessBox() {

        String[] endProcessNameArray = new String[processListMAXLength];
        String[] endProcessPriorityArray = new String[processListMAXLength];
        String[] endProcessCPURunningTimeArray = new String[processListMAXLength];
        String[] endProcessStateArray = new String[processListMAXLength];
        String[] endProcessBlockingTimeArray = new String[processListMAXLength];
        String[] endProcessOrdersArray = new String[processListMAXLength];

        for (int i = 0; i < endProcessList.length; i++) {
            endProcessNameArray[i] = endProcessList[i].processName;
            endProcessPriorityArray[i] = String.valueOf(endProcessList[i].priority);
            endProcessCPURunningTimeArray[i] = String.valueOf(endProcessList[i].CPURunningTime);
            endProcessStateArray[i] = endProcessList[i].state;
            endProcessBlockingTimeArray[i] = "0";
            endProcessOrdersArray[i] = String.valueOf(endProcessList[i].orders);
        }

        listEndProcess1.setListData(endProcessNameArray);
        listEndProcess2.setListData(endProcessPriorityArray);
        listEndProcess3.setListData(endProcessCPURunningTimeArray);
        listEndProcess4.setListData(endProcessStateArray);
        listEndProcess5.setListData(endProcessBlockingTimeArray);
        listEndProcess6.setListData(endProcessOrdersArray);

    }

    //检测到内存/物理块中是否有该页面，-1表示没有
    public int findExist(int curPage) {
        for (int i = 0; i < 4; i++) {
            if (runProcess.physicalBlocks[i].pageNum == curPage)
                //检测到内存中有该页面，返回block中的位置
                return i;
        }
        return -1;
    }

    //找到空闲的物理块
    public int findSpace() {
        for (int i = 0; i < 4; i++) {
            if (runProcess.physicalBlocks[i].pageNum == -1)
                //找到空闲的block，返回block中的位置
                return i;
        }
        return -1;
    }

    //找到应予置换页面，返回BLOCK中位置，越少使用的nextAccessInstruction越大
    public int findReplace() {
        int positionIndex = 0;
        for (int i = 0; i < 4; i++) {
            if (runProcess.physicalBlocks[i].nextAccessInstruction > runProcess.physicalBlocks[positionIndex].nextAccessInstruction)
                //找到应予置换页面，返回BLOCK中位置
                positionIndex = i;
        }
        return positionIndex;
    }

    //打印四个块中页号
    public void display() {
        System.out.println("*****");
        for (int i = 0; i < 4; i++) {
            if (runProcess.physicalBlocks[i].pageNum != -1) {
                System.out.print(runProcess.physicalBlocks[i].pageNum + " ");
            }
        }
        System.out.println("*****");
    }


    //指令地址随机生成
    public void createRandomAddress() {
        Random r = new Random();
        for (int i = 0; i < RandomMAXNumbers; i++) {
            ordersAddress[i] = r.nextInt(RandomMAXNumbers);
        }
    }

    //LRU算法
    public void LRU(int temp) {
        int exist, space, position;
        int curPage;

        pc = temp;
        curPage = pc / MaximumPageCapacity;
        //检测到内存/物理块中是否有该页面，-1表示没有，其他值表示该页存在且位置在这
        exist = findExist(curPage);
        //没有该页，进行存该页操作
        if (exist == -1) {
            n++;
            //找到空闲的物理块，没有空闲是-1，有空闲则是该物理块位置
            space = findSpace();
            //space != -1 即有空闲，将该页存入
            if (space != -1) {
                runProcess.physicalBlocks[space].pageNum = curPage;
                //打印四个块中页号
                display();

            } else {
                //找到应予置换页面，返回BLOCK中位置
                position = findReplace();
                runProcess.physicalBlocks[position].pageNum = curPage;
                //打印四个块中页号
                display();
            }
        } else {
            //最近使用过，赋值为0
            runProcess.physicalBlocks[exist].nextAccessInstruction = 0;
        }
        //每块中存在的页面未使用时长加1，下次寻找该值最大的块置换出去
        //规定越少使用的nextAccessInstruction越大，越多使用的nextAccessInstruction越小
        for (int j = 0; j < 4; j++) {
            if (j == exist) continue;
            runProcess.physicalBlocks[j].nextAccessInstruction++;
        }


        System.out.println("缺页次数: " + n);
        System.out.println("缺页率: " + ((n * 0.1) / (orderLength * 0.1)));
    }

    //在物理块中显示
    public void showPhysicalBlock() {
        if (runProcess != null && runProcess.processName != null) {
            t1.setText(runProcess.physicalBlocksIdentifier[0]+"");
            t2.setText(runProcess.physicalBlocksIdentifier[1]+"");
            t3.setText(runProcess.physicalBlocksIdentifier[2]+"");
            t4.setText(runProcess.physicalBlocksIdentifier[3]+"");
            if (runProcess.physicalBlocks[0].pageNum != -1) {
                //printf(" %02d",block[i].pagenum);
                physicalBlock1.setText(String.valueOf(runProcess.physicalBlocks[0].pageNum));
            } else {
                physicalBlock1.setText("#");
            }
            if (runProcess.physicalBlocks[1].pageNum != -1) {
                physicalBlock2.setText(String.valueOf(runProcess.physicalBlocks[1].pageNum));
            } else {
                physicalBlock2.setText("#");
            }
            if (runProcess.physicalBlocks[2].pageNum != -1) {
                physicalBlock3.setText(String.valueOf(runProcess.physicalBlocks[2].pageNum));
            } else {
                physicalBlock3.setText("#");
            }
            if (runProcess.physicalBlocks[3].pageNum != -1) {
                physicalBlock4.setText(String.valueOf(runProcess.physicalBlocks[3].pageNum));
            } else {
                physicalBlock4.setText("#");
            }
        }

    }

    //指令的具体内容
    public String ordersExecution() {
        String[] ordersType = {"MEMORY WRITE IN", "MEMORY WRITE OUT", "INPUT", "OUTPUT"};
        Random random = new Random();
        int ordersTypeIndex;
        ordersTypeIndex = random.nextInt(4);
        orderFrame.setText(ordersType[ordersTypeIndex]);
        return ordersType[ordersTypeIndex];
    }


    //int数组转String数组，过程中/MaximumPageCapacity
    public void intArrayTOStringArray(int[] temp, String[] result) {
        for (int i = 0; i < temp.length; i++) {
            result[i] = String.valueOf(temp[i] / MaximumPageCapacity);
        }
    }

    //int数组转String数组
    public void intArrayTOString(int[] temp, String[] result) {
        for (int i = 0; i < temp.length; i++) {
            result[i] = String.valueOf(temp[i]);
        }
    }

    //初始化进程对应的物理块数组
    public void InitializeProcessPageHome(String[] result) {
        for (int i = 0; i < result.length; i++) {
            result[i] = "-";
        }
    }

    //改变进程对应的物理块
    public void changeProcessPageHome(int[] temp, String[] resultProcessPageHome) {
        for (int i = 0; i < temp.length; i++) {
            //检测到物理块中是否有该页面,参数是页号
            int exist = findExist(temp[i] / MaximumPageCapacity);
            if (exist != -1) {
                resultProcessPageHome[i] = runProcess.physicalBlocksIdentifier[exist] + "";
            } else {
                resultProcessPageHome[i] = "*";

            }
        }
    }

    //selectPhysicalBlockFromMemory
    public void selectPhysicalBlockFromMemory() {
        final int memoryBlocksMAXNumber = 15;
        final int physicalMemoryBlocksMAXNumber = 4;
        systemOccupancy = (int) Math.round(Math.random() * (11 - 5) + 5);
        int[] memoryBlocks = new int[memoryBlocksMAXNumber];
        int[] physicalMemoryBlocks = new int[memoryBlocksMAXNumber];

        for (int i = 0; i < memoryBlocksMAXNumber; i++) {
            memoryBlocks[i] = i;
            physicalMemoryBlocks[i] = 1;
            if (i > systemOccupancy) {
                physicalMemoryBlocks[i] = 0;
            }
        }

        String[] memoryBlocksString = new String[memoryBlocksMAXNumber];
        intArrayTOString(memoryBlocks, memoryBlocksString);
        String[] physicalMemoryBlocksString = new String[memoryBlocksMAXNumber];
        intArrayTOString(physicalMemoryBlocks, physicalMemoryBlocksString);

        listMemory1.setListData(memoryBlocksString);
        listMemory2.setListData(physicalMemoryBlocksString);
    }

    //定义生成随机数并且装入集合容器的方法
    //方法的形参列表分别为：生成随机数的个数、生成随机数的值的范围最小值为start(包含start)、值得范围最大值为end(不包含end)  可取值范围可表示为[start,end)
    public List<Integer> getRandomNumList(int nums, int start, int end) {
        //1.创建集合容器对象
        List<Integer> list = new ArrayList();
        //2.创建Random对象
        Random r = new Random();
        //循环将得到的随机数进行判断，如果随机数不存在于集合中，则将随机数放入集合中，如果存在，则将随机数丢弃不做操作，进行下一次循环，直到集合长度等于nums
        while (list.size() != nums) {
            int num = r.nextInt(end - start) + start;
            if (!list.contains(num)) {
                list.add(num);
            }
        }

        return list;
    }

    public void bubbleSorting(int[] numbers) {
        //需进行length-1次冒泡
        for (int i = 0; i < numbers.length - 1; i++) {
            for (int j = 0; j < numbers.length - 1 - i; j++) {
                if (numbers[j] > numbers[j + 1]) {
                    int temp = numbers[j];
                    numbers[j] = numbers[j + 1];
                    numbers[j + 1] = temp;
                }
            }
        }
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("MyTestForm");
        frame.setContentPane(new MyTest().jaaa);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
        frame.setSize(3000, 3000);
    }

    private void createUIComponents() {
        // TODO: place custom component creation code here
    }
}
